import {createRouter, createWebHistory} from 'vue-router'
import threeRouter from './modules/three'
import cssRouter from './modules/css'
import mapRouter from './modules/map'
import Temp404 from '@/views/global/temp/404.vue'
import Layout from "@/views/index.vue";


const routes = [
    {
        path: '',
        name: 'index',
        redirect: '/three',
        component: Layout,
        children: [
            cssRouter,
            threeRouter,
            mapRouter,
        ]
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: Temp404 },
    // { path: '', redirect: '/demo' },
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
});

router.beforeEach((to, from , next) => {
    const rou = router.getRoutes().filter(v => v.name === to.name)?.[0]
    if (rou.children?.length > 0) {
        const deep = (r) => {
            if (r.children?.length > 0) {
                deep(r.children[0])
            } else {
                next({
                    name: r.name,
                    replace: true
                })
            }
        }
        deep(rou)
    } else {
        next()
    }
})
export default router;
